<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/layers/eltwise_layer.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/layers</a> - eltwise_layer.cpp<span style="font-size: 80%;"> (source / <a href="eltwise_layer.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">79</td>
            <td class="headerCovTableEntryLo">2.5 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:25:26</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">16</td>
            <td class="headerCovTableEntryLo">12.5 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &lt;cfloat&gt;</a>
<span class="lineNum">       2 </span>            : #include &lt;vector&gt;
<span class="lineNum">       3 </span>            : 
<span class="lineNum">       4 </span>            : #include &quot;caffe/layers/eltwise_layer.hpp&quot;
<span class="lineNum">       5 </span>            : #include &quot;caffe/util/math_functions.hpp&quot;
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : namespace caffe {
<span class="lineNum">       8 </span>            : 
<span class="lineNum">       9 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      10 </span><span class="lineNoCov">          0 : void EltwiseLayer&lt;Dtype&gt;::LayerSetUp(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      11 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      12 </span><span class="lineNoCov">          0 :   CHECK(this-&gt;layer_param().eltwise_param().coeff_size() == 0</span>
<span class="lineNum">      13 </span>            :       || this-&gt;layer_param().eltwise_param().coeff_size() == bottom.size()) &lt;&lt;
<span class="lineNum">      14 </span>            :       &quot;Eltwise Layer takes one coefficient per bottom blob.&quot;;
<span class="lineNum">      15 </span><span class="lineNoCov">          0 :   CHECK(!(this-&gt;layer_param().eltwise_param().operation()</span>
<span class="lineNum">      16 </span>            :       == EltwiseParameter_EltwiseOp_PROD
<span class="lineNum">      17 </span>            :       &amp;&amp; this-&gt;layer_param().eltwise_param().coeff_size())) &lt;&lt;
<span class="lineNum">      18 </span>            :       &quot;Eltwise layer only takes coefficients for summation.&quot;;
<span class="lineNum">      19 </span><span class="lineNoCov">          0 :   op_ = this-&gt;layer_param_.eltwise_param().operation();</span>
<span class="lineNum">      20 </span>            :   // Blob-wise coefficients for the elementwise operation.
<span class="lineNum">      21 </span><span class="lineNoCov">          0 :   coeffs_ = vector&lt;Dtype&gt;(bottom.size(), 1);</span>
<span class="lineNum">      22 </span><span class="lineNoCov">          0 :   if (this-&gt;layer_param().eltwise_param().coeff_size()) {</span>
<span class="lineNum">      23 </span><span class="lineNoCov">          0 :     for (int i = 0; i &lt; bottom.size(); ++i) {</span>
<span class="lineNum">      24 </span><span class="lineNoCov">          0 :       coeffs_[i] = this-&gt;layer_param().eltwise_param().coeff(i);</span>
<span class="lineNum">      25 </span>            :     }
<span class="lineNum">      26 </span>            :   }
<span class="lineNum">      27 </span><span class="lineNoCov">          0 :   stable_prod_grad_ = this-&gt;layer_param_.eltwise_param().stable_prod_grad();</span>
<span class="lineNum">      28 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      29 </span>            : 
<span class="lineNum">      30 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      31 </span><span class="lineNoCov">          0 : void EltwiseLayer&lt;Dtype&gt;::Reshape(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      32 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      33 </span><span class="lineNoCov">          0 :   for (int i = 1; i &lt; bottom.size(); ++i) {</span>
<span class="lineNum">      34 </span><span class="lineNoCov">          0 :     CHECK(bottom[0]-&gt;shape() == bottom[i]-&gt;shape())</span>
<span class="lineNum">      35 </span><span class="lineNoCov">          0 :         &lt;&lt; &quot;bottom[0]: &quot; &lt;&lt; bottom[0]-&gt;shape_string()</span>
<span class="lineNum">      36 </span><span class="lineNoCov">          0 :         &lt;&lt; &quot;, bottom[&quot; &lt;&lt; i &lt;&lt; &quot;]: &quot; &lt;&lt; bottom[i]-&gt;shape_string();</span>
<span class="lineNum">      37 </span>            :   }
<span class="lineNum">      38 </span><span class="lineNoCov">          0 :   top[0]-&gt;ReshapeLike(*bottom[0]);</span>
<span class="lineNum">      39 </span>            :   // If max operation, we will initialize the vector index part.
<span class="lineNum">      40 </span><span class="lineNoCov">          0 :   if (this-&gt;layer_param_.eltwise_param().operation() ==</span>
<span class="lineNum">      41 </span>            :       EltwiseParameter_EltwiseOp_MAX &amp;&amp; top.size() == 1) {
<span class="lineNum">      42 </span><span class="lineNoCov">          0 :     max_idx_.Reshape(bottom[0]-&gt;shape());</span>
<span class="lineNum">      43 </span>            :   }
<span class="lineNum">      44 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      45 </span>            : 
<span class="lineNum">      46 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      47 </span><span class="lineNoCov">          0 : void EltwiseLayer&lt;Dtype&gt;::Forward_cpu(</span>
<span class="lineNum">      48 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      49 </span>            :   int* mask = NULL;
<span class="lineNum">      50 </span>            :   const Dtype* bottom_data_a = NULL;
<span class="lineNum">      51 </span>            :   const Dtype* bottom_data_b = NULL;
<span class="lineNum">      52 </span><span class="lineNoCov">          0 :   const int count = top[0]-&gt;count();</span>
<span class="lineNum">      53 </span><span class="lineNoCov">          0 :   Dtype* top_data = top[0]-&gt;mutable_cpu_data();</span>
<span class="lineNum">      54 </span><span class="lineNoCov">          0 :   switch (op_) {</span>
<span class="lineNum">      55 </span>            :   case EltwiseParameter_EltwiseOp_PROD:
<span class="lineNum">      56 </span><span class="lineNoCov">          0 :     caffe_mul(count, bottom[0]-&gt;cpu_data(), bottom[1]-&gt;cpu_data(), top_data);</span>
<span class="lineNum">      57 </span><span class="lineNoCov">          0 :     for (int i = 2; i &lt; bottom.size(); ++i) {</span>
<span class="lineNum">      58 </span><span class="lineNoCov">          0 :       caffe_mul(count, top_data, bottom[i]-&gt;cpu_data(), top_data);</span>
<span class="lineNum">      59 </span>            :     }
<span class="lineNum">      60 </span>            :     break;
<span class="lineNum">      61 </span>            :   case EltwiseParameter_EltwiseOp_SUM:
<span class="lineNum">      62 </span><span class="lineNoCov">          0 :     caffe_set(count, Dtype(0), top_data);</span>
<span class="lineNum">      63 </span>            :     // TODO(shelhamer) does BLAS optimize to sum for coeff = 1?
<span class="lineNum">      64 </span><span class="lineNoCov">          0 :     for (int i = 0; i &lt; bottom.size(); ++i) {</span>
<span class="lineNum">      65 </span><span class="lineNoCov">          0 :       caffe_axpy(count, coeffs_[i], bottom[i]-&gt;cpu_data(), top_data);</span>
<span class="lineNum">      66 </span>            :     }
<span class="lineNum">      67 </span>            :     break;
<span class="lineNum">      68 </span>            :   case EltwiseParameter_EltwiseOp_MAX:
<span class="lineNum">      69 </span>            :     // Initialize
<span class="lineNum">      70 </span><span class="lineNoCov">          0 :     mask = max_idx_.mutable_cpu_data();</span>
<span class="lineNum">      71 </span><span class="lineNoCov">          0 :     caffe_set(count, -1, mask);</span>
<span class="lineNum">      72 </span><span class="lineNoCov">          0 :     caffe_set(count, Dtype(-FLT_MAX), top_data);</span>
<span class="lineNum">      73 </span>            :     // bottom 0 &amp; 1
<span class="lineNum">      74 </span><span class="lineNoCov">          0 :     bottom_data_a = bottom[0]-&gt;cpu_data();</span>
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :     bottom_data_b = bottom[1]-&gt;cpu_data();</span>
<span class="lineNum">      76 </span><span class="lineNoCov">          0 :     for (int idx = 0; idx &lt; count; ++idx) {</span>
<span class="lineNum">      77 </span><span class="lineNoCov">          0 :       if (bottom_data_a[idx] &gt; bottom_data_b[idx]) {</span>
<span class="lineNum">      78 </span><span class="lineNoCov">          0 :         top_data[idx] = bottom_data_a[idx];  // maxval</span>
<span class="lineNum">      79 </span><span class="lineNoCov">          0 :         mask[idx] = 0;  // maxid</span>
<span class="lineNum">      80 </span>            :       } else {
<span class="lineNum">      81 </span><span class="lineNoCov">          0 :         top_data[idx] = bottom_data_b[idx];  // maxval</span>
<span class="lineNum">      82 </span><span class="lineNoCov">          0 :         mask[idx] = 1;  // maxid</span>
<span class="lineNum">      83 </span>            :       }
<span class="lineNum">      84 </span>            :     }
<span class="lineNum">      85 </span>            :     // bottom 2++
<span class="lineNum">      86 </span><span class="lineNoCov">          0 :     for (int blob_idx = 2; blob_idx &lt; bottom.size(); ++blob_idx) {</span>
<span class="lineNum">      87 </span><span class="lineNoCov">          0 :       bottom_data_b = bottom[blob_idx]-&gt;cpu_data();</span>
<span class="lineNum">      88 </span><span class="lineNoCov">          0 :       for (int idx = 0; idx &lt; count; ++idx) {</span>
<span class="lineNum">      89 </span><span class="lineNoCov">          0 :         if (bottom_data_b[idx] &gt; top_data[idx]) {</span>
<span class="lineNum">      90 </span><span class="lineNoCov">          0 :           top_data[idx] = bottom_data_b[idx];  // maxval</span>
<span class="lineNum">      91 </span><span class="lineNoCov">          0 :           mask[idx] = blob_idx;  // maxid</span>
<span class="lineNum">      92 </span>            :         }
<span class="lineNum">      93 </span>            :       }
<span class="lineNum">      94 </span>            :     }
<span class="lineNum">      95 </span>            :     break;
<span class="lineNum">      96 </span>            :   default:
<span class="lineNum">      97 </span><span class="lineNoCov">          0 :     LOG(FATAL) &lt;&lt; &quot;Unknown elementwise operation.&quot;;</span>
<span class="lineNum">      98 </span>            :   }
<span class="lineNum">      99 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">     100 </span>            : 
<span class="lineNum">     101 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     102 </span><span class="lineNoCov">          0 : void EltwiseLayer&lt;Dtype&gt;::Backward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,</span>
<span class="lineNum">     103 </span>            :     const vector&lt;bool&gt;&amp; propagate_down, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom) {
<span class="lineNum">     104 </span>            :   const int* mask = NULL;
<span class="lineNum">     105 </span><span class="lineNoCov">          0 :   const int count = top[0]-&gt;count();</span>
<span class="lineNum">     106 </span><span class="lineNoCov">          0 :   const Dtype* top_data = top[0]-&gt;cpu_data();</span>
<span class="lineNum">     107 </span><span class="lineNoCov">          0 :   const Dtype* top_diff = top[0]-&gt;cpu_diff();</span>
<span class="lineNum">     108 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; bottom.size(); ++i) {</span>
<span class="lineNum">     109 </span><span class="lineNoCov">          0 :     if (propagate_down[i]) {</span>
<span class="lineNum">     110 </span><span class="lineNoCov">          0 :       const Dtype* bottom_data = bottom[i]-&gt;cpu_data();</span>
<span class="lineNum">     111 </span><span class="lineNoCov">          0 :       Dtype* bottom_diff = bottom[i]-&gt;mutable_cpu_diff();</span>
<span class="lineNum">     112 </span><span class="lineNoCov">          0 :       switch (op_) {</span>
<span class="lineNum">     113 </span>            :       case EltwiseParameter_EltwiseOp_PROD:
<span class="lineNum">     114 </span><span class="lineNoCov">          0 :         if (stable_prod_grad_) {</span>
<span class="lineNum">     115 </span>            :           bool initialized = false;
<span class="lineNum">     116 </span><span class="lineNoCov">          0 :           for (int j = 0; j &lt; bottom.size(); ++j) {</span>
<span class="lineNum">     117 </span><span class="lineNoCov">          0 :             if (i == j) { continue; }</span>
<span class="lineNum">     118 </span><span class="lineNoCov">          0 :             if (!initialized) {</span>
<span class="lineNum">     119 </span><span class="lineNoCov">          0 :               caffe_copy(count, bottom[j]-&gt;cpu_data(), bottom_diff);</span>
<span class="lineNum">     120 </span>            :               initialized = true;
<span class="lineNum">     121 </span>            :             } else {
<span class="lineNum">     122 </span><span class="lineNoCov">          0 :               caffe_mul(count, bottom[j]-&gt;cpu_data(), bottom_diff,</span>
<span class="lineNum">     123 </span>            :                         bottom_diff);
<span class="lineNum">     124 </span>            :             }
<span class="lineNum">     125 </span>            :           }
<span class="lineNum">     126 </span>            :         } else {
<span class="lineNum">     127 </span><span class="lineNoCov">          0 :           caffe_div(count, top_data, bottom_data, bottom_diff);</span>
<span class="lineNum">     128 </span>            :         }
<span class="lineNum">     129 </span><span class="lineNoCov">          0 :         caffe_mul(count, bottom_diff, top_diff, bottom_diff);</span>
<span class="lineNum">     130 </span><span class="lineNoCov">          0 :         break;</span>
<span class="lineNum">     131 </span>            :       case EltwiseParameter_EltwiseOp_SUM:
<span class="lineNum">     132 </span><span class="lineNoCov">          0 :         if (coeffs_[i] == Dtype(1)) {</span>
<span class="lineNum">     133 </span><span class="lineNoCov">          0 :           caffe_copy(count, top_diff, bottom_diff);</span>
<span class="lineNum">     134 </span>            :         } else {
<span class="lineNum">     135 </span><span class="lineNoCov">          0 :           caffe_cpu_scale(count, coeffs_[i], top_diff, bottom_diff);</span>
<span class="lineNum">     136 </span>            :         }
<span class="lineNum">     137 </span>            :         break;
<span class="lineNum">     138 </span>            :       case EltwiseParameter_EltwiseOp_MAX:
<span class="lineNum">     139 </span><span class="lineNoCov">          0 :         mask = max_idx_.cpu_data();</span>
<span class="lineNum">     140 </span><span class="lineNoCov">          0 :         for (int index = 0; index &lt; count; ++index) {</span>
<span class="lineNum">     141 </span>            :           Dtype gradient = 0;
<span class="lineNum">     142 </span><span class="lineNoCov">          0 :           if (mask[index] == i) {</span>
<span class="lineNum">     143 </span><span class="lineNoCov">          0 :             gradient += top_diff[index];</span>
<span class="lineNum">     144 </span>            :           }
<span class="lineNum">     145 </span><span class="lineNoCov">          0 :           bottom_diff[index] = gradient;</span>
<span class="lineNum">     146 </span>            :         }
<span class="lineNum">     147 </span>            :         break;
<span class="lineNum">     148 </span>            :       default:
<span class="lineNum">     149 </span><span class="lineNoCov">          0 :         LOG(FATAL) &lt;&lt; &quot;Unknown elementwise operation.&quot;;</span>
<span class="lineNum">     150 </span>            :       }
<span class="lineNum">     151 </span>            :     }
<span class="lineNum">     152 </span>            :   }
<span class="lineNum">     153 </span><span class="lineNoCov">          0 : }</span>
<a name="154"><span class="lineNum">     154 </span>            : </a>
<span class="lineNum">     155 </span>            : #ifdef CPU_ONLY
<span class="lineNum">     156 </span><span class="lineNoCov">          0 : STUB_GPU(EltwiseLayer);</span>
<span class="lineNum">     157 </span>            : #endif
<a name="158"><span class="lineNum">     158 </span>            : </a>
<span class="lineNum">     159 </span>            : INSTANTIATE_CLASS(EltwiseLayer);
<a name="160"><span class="lineNum">     160 </span><span class="lineCov">          3 : REGISTER_LAYER_CLASS(Eltwise);</span></a>
<span class="lineNum">     161 </span>            : 
<span class="lineNum">     162 </span><span class="lineCov">          3 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
